"use strict";

function htmlspecialchars(str) {
    var s = "";
    if (str.length == 0) return "";
    for (var i = 0; i < str.length; i++) {
        switch (str.substr(i, 1)) {
            case "<":
                s += "&lt;";break;
            case ">":
                s += "&gt;";break;
            case "&":
                s += "&amp;";break;
            case " ":
                if (str.substr(i + 1, 1) == " ") {
                    s += " &nbsp;";
                    i++;
                } else s += " ";
                break;
            case "\"":
                s += "&quot;";break;
            // case "\n": s += "<br>"; break;
            default:
                s += str.substr(i, 1);break;
        }
    }
    return s;
}

var initlize = {};

function selectTab(forele) {
    var curpage = document.getElementById(forele);
    if (lastOpenTab && lastOpenTab === curpage) {
        return false;
    }
    curpage.classList.add("open");
    document.querySelector("[for=" + forele + "]").classList.add("active");
    if (lastOpenTab) {
        lastOpenTab.classList.remove("open");
        document.querySelector("[for=" + lastOpenTab.id + "]").classList.remove("active");
    }
    lastOpenTab = curpage;
    var previews = curpage.querySelectorAll(".aux-preview-code");
    if (previews) {
        if (!initlize[forele]) {
            Array.prototype.slice.call(previews).forEach(function (v) {
                if (window.Prism) {
                    var code = v.previousSibling.previousSibling.outerHTML;
                    var b = document.createElement("button");
                    b.textContent = "copy";
                    b.addEventListener("click", function () {
                        copyToClipboard(code);
                    });
                    v.appendChild(b);
                    v.children[0].$innerHTML = Prism.highlight(code, Prism["languages"]["markup"], "markup");
                    v.children[0].innerHTML = v.children[0].$innerHTML;
                    var s = document.createElement("button");
                    s.textContent = "查看结构代码";
                    v.insertAdjacentElement('afterend', s);
                    s.addEventListener("click", function () {
                        var isActive = v.classList.toggle("active");
                        if (isActive) {
                            s.textContent = "关闭结构代码";
                        } else {
                            s.textContent = "查看结构代码";
                        }
                    });
                }
            });
            initlize[forele] = true;
        }
    }
}

var lastOpenTab = null;
var links = document.querySelectorAll("#nav a");
if (links) {
    Array.prototype.slice.call(links).forEach(function (v) {
        var forele = v.getAttribute("for");
        v.addEventListener("click", function (e) {
            e.preventDefault();
            selectTab(forele);
        });
    });
};

var copyToClipboard = function copyToClipboard(str) {
    var el = document.createElement('textarea');
    el.value = str;
    el.setAttribute('readonly', '');
    el.style.position = 'absolute';
    el.style.left = '-9999px';
    document.body.appendChild(el);
    var selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
    if (selected) {
        document.getSelection().removeAllRanges();
        document.getSelection().addRange(selected);
    }
};

selectTab("pongyouquan");